package org.op.editor;

import java.util.List;

import javax.swing.text.Document;

import org.op.FrameView;
import org.op.service.config.ConfigurationService;
import org.op.service.doc.DocumentConversionService;
import org.op.service.doc.ReplaceTextService;
import org.op.service.log.LoggerService;
import org.op.util.MessageController;

public class SearchAndReplaceControllerImpl implements
		SearchAndReplaceController {

	private SearchDialogView searchView;
	private ReplaceDialogView replaceView;
	private EditorComponentView editorView;
	private DocumentConversionService docConverter;
	private ReplaceTextService replacementService;
	private FrameView frameView;
	private MessageController messageController;
	private ConfigurationService configService;
	private LoggerService logger;
	
	public void replaceText() {
		
		try {
			String title = messageController.getLabel("replace.dialog.title");
			String searchLabel = messageController.getLabel("replace.dialog.search.label");
			String replaceLabel = messageController.getLabel("replace.dialog.replace.label");
			String[] sarText = replaceView.openAndGet(frameView.getFrame(), title, searchLabel, replaceLabel);
			String searchText = sarText[0];
			String replaceText = sarText[1];
			
			if (searchText!=null && replaceText!=null) {
				Document doc = editorView.getCurrentPane().getDocument();
				List<String> content = docConverter.documentToText(doc);
				
				content = replacementService.replaceText(content, searchText, replaceText);
				doc = docConverter.textToDocument(content);
				
				editorView.getCurrentPane().setDocument(doc);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}

	public void searchForText() {
		try {
			String searchText = searchView.openAndGet();
			
			Document doc = editorView.getCurrentPane().getDocument();
			int caratPos = editorView.getCurrentPane().getCaretPosition();
			int docLen = doc.getLength();
			
			String endText = doc.getText(caratPos, (docLen - caratPos));
			int nextLoc = endText.indexOf(searchText);
		
			if (nextLoc>=0) {
				caratPos = caratPos + nextLoc;
				updateSelected(caratPos, searchText.length());

			} else {
				String beginText = doc.getText(0, caratPos);
				nextLoc = beginText.indexOf(searchText);
				if (nextLoc>=0) {
					updateSelected(nextLoc, searchText.length());
				} else {
					String notFoundMessage = messageController.getMessage("search.text.not.found");
					frameView.showInfoDialog(notFoundMessage);
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void updateSelected(int caratPos, int length) throws Exception {
		editorView.getCurrentPane().setCaretPosition(caratPos);
		editorView.getCurrentPane().setSelectionStart(caratPos);
		editorView.getCurrentPane().setSelectionEnd(caratPos + length);
	}

	public SearchDialogView getSearchView() {
		return searchView;
	}

	public void setSearchView(SearchDialogView searchView) {
		this.searchView = searchView;
	}

	public ReplaceDialogView getReplaceView() {
		return replaceView;
	}

	public void setReplaceView(ReplaceDialogView replaceView) {
		this.replaceView = replaceView;
	}

	public EditorComponentView getEditorView() {
		return editorView;
	}

	public void setEditorView(EditorComponentView editorView) {
		this.editorView = editorView;
	}

	public DocumentConversionService getDocConverter() {
		return docConverter;
	}

	public void setDocConverter(DocumentConversionService docConverter) {
		this.docConverter = docConverter;
	}

	public ReplaceTextService getReplacementService() {
		return replacementService;
	}

	public void setReplacementService(ReplaceTextService replacementService) {
		this.replacementService = replacementService;
	}

	public FrameView getFrameView() {
		return frameView;
	}

	public void setFrameView(FrameView frameView) {
		this.frameView = frameView;
	}

	public MessageController getMessageController() {
		return messageController;
	}

	public void setMessageController(MessageController messageController) {
		this.messageController = messageController;
	}

	public ConfigurationService getConfigService() {
		return configService;
	}

	public void setConfigService(ConfigurationService configService) {
		this.configService = configService;
	}

	public LoggerService getLogger() {
		return logger;
	}

	public void setLogger(LoggerService logger) {
		this.logger = logger;
	}

	
}
